appModule.controller("SessionConfigEditController", ['$rootScope', '$scope', '$ionicPopup', '$ionicSideMenuDelegate', 'sessionConfigService', function($rootScope, $scope, $ionicPopup, $ionicSideMenuDelegate, sessionConfigService) {
    // 当前会话
    $scope.curSshSession = null;
    // 所有会话
    $scope.sshSessions = {};

    $rootScope.sshSessionConfig = new SSHSessionConfig();
    $scope.saveSSHSessionConfig = function() {
        if($scope.editSessionConfigForm.$invalid) {
            $ionicPopup.alert({
                title: '输入有误'
            });
            return ;
        }
        var oldName = $rootScope.sshSessionConfig.oldName;
        delete $rootScope.sshSessionConfig.oldName;
        if(sessionConfigService.contains($rootScope.sshSessionConfig.name) && oldName != $rootScope.sshSessionConfig.name) {
            $ionicPopup.alert({
                title: '连接名重复'
            });   // 提示直接更新
            return ;
        }

        // hide隐藏侧边栏
        $ionicSideMenuDelegate.toggleLeft();

        if($scope.savePassword) {
            // 保存连接配置
            sessionConfigService.save($rootScope.sshSessionConfig);
            if(oldName != $rootScope.sshSessionConfig.name) {   // 新旧不一样
                sessionConfigService.delete(oldName);
            }
            // 更新会话列表
            $rootScope.sessionConfigs = sessionConfigService.getAll();
        } else {

        }

        // 打开终端
        // $scope.openTerminal($rootScope.sshSessionConfig);
        $rootScope.sshSessionConfig = new SSHSessionConfig();
    };

    // 打开连接
    $scope.openTerminal = function(sshSessionConfig) {
        var terminalName = sshSessionConfig.name;
        (function () {
            var tempTerminalName = terminalName;
            for (var i = 1; ; i++) {
                var isRepeat = false;
                $.each($scope.sshSessions, function () {
                    if (this.terminalName == tempTerminalName) {
                        var suffix = "(" + i + ")";
                        tempTerminalName = terminalName + suffix;
                        isRepeat = true;
                        return false;
                    }
                });
                if (!isRepeat) {
                    break;
                }
            }
            terminalName = tempTerminalName;
        }());

        var sshSession = new SSHSession(sshSessionConfig);
        // 更新当前终端
        $scope.curSshSession = sshSession;
        $scope.sshSessions[sshSession.terminalName] = sshSession;
        indexService.openTerminal(sshSession);
        $("#sessionConfigsModal").modal("hide");
    };

    // 切换终端
    $scope.changeTerminal = function(sshSession) {
        $scope.curSshSession = sshSession;

        var terminal = $("#command-response").find("[terminal-name='" + sshSession.terminalName + "']");
        $(".terminal-wrap").hide();
        terminal.show();
    };   // End changeTerminal

    // 关闭终端
    $scope.closeTerminal = function(session) {
        // 关闭终端
        session.close();
        var terminal = $("#command-response").find("[terminal-name='" + terminalName + "']");
        terminal.remove();

        // 从数组移除当前会话
        $.each($scope.sshSessions, function(i) {
            if(this.terminalName == session.terminalName) {
                $scope.sshSessions.splice(i, 1);
                delete $scope.sshSessions[session.terminalName];
                return false;
            }
        });

        // 显示下一个会话
        if($scope.curSshSession.terminalName == session.terminalName) {
            $scope.curSshSession = null;
            if($scope.sshSessions.length > 0) {
                // 需要显示其他终端
                $scope.changeTerminal($scope.sshSessions[0]);
            }
        }
    };   // End closeTerminal
}]);